package com.dartfar.test.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.base.vo.FunctionVo;
import com.dartfar.test.model.BorrowMoney;
import com.dartfar.test.service.BorrowMoneyService;

@Scope("prototype")
@Component("borrowMoneyAction")
public class BorrowMoneyAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(BorrowMoneyAction.class);
	
	@Resource
	private BorrowMoneyService borrowMoneyService;	
	
	private int id;
	private Double bmNum;
	private String bmContent;
	
	private BorrowMoney borrowMoney;
	
	/**
	 * 页面跳转
	 * @return
	 */
	public String show(){
		//获取权限列表
		//functions = getAutFunctions();
		return "show";
	}
	
	public String view(){
		borrowMoney = borrowMoneyService.get(id);
		return "view";
	}
	
	/**
	 * 获取功能
	 */
	public void loadAutBtnData(){
		logger.info("BorrowMoneyAction");
		try{
			List<FunctionVo> functionsVo = getAutFunctions();
			String jsonResult = JSONArray.fromObject(functionsVo).toString();
			logger.info("jsonResult: " + jsonResult);
		
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

	public String add(){
		BorrowMoney borrowMoney = new BorrowMoney();
		borrowMoney.setNum(bmNum);
		borrowMoney.setDes(bmContent);
		System.out.println("dddd: " + curDepart());
		borrowMoney.setDepart(curDepart().getName());
		borrowMoney.setDepartId(String.valueOf(curDepart().getId()));
		borrowMoney.setPerson(curPerson().getName());
		borrowMoney.setPersonId(String.valueOf(curPerson().getPersonId()));
		borrowMoney.setCreateDate(sdf.format(new Date()));
		//borrowMoney.setWorkflowStatus(workflowStatus)
		borrowMoneyService.save(borrowMoney);
		return SUCCESS;
	}
	
	public String edit(){
		BorrowMoney borrowMoney = borrowMoneyService.get(id);
		borrowMoney.setNum(bmNum);
		borrowMoney.setDes(bmContent);
		borrowMoneyService.update(borrowMoney);
		return SUCCESS;
	}
	
	/**
	 * 获取借款申请列表
	 * @return
	 */
	public String loadBorrowMoneyData(){
		DataGridVo<BorrowMoney> dataGridVo = new DataGridVo<BorrowMoney>();
		try{
			//获取列表
			List<BorrowMoney> list = borrowMoneyService.getListForPage(pageNo, pageSize);
			Long count = borrowMoneyService.getCount();
			
			dataGridVo.setRows(list);
			dataGridVo.setTotal(count);
			
			String jsonResult = JSONObject.fromObject(dataGridVo).toString();
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public void loadBorrowMoneyDataById(){
		try {
			borrowMoney = borrowMoneyService.get(id);
			String jsonResult = JSONObject.fromObject(borrowMoney).toString();
			logger.info("jsonResult: " + jsonResult);
			response.getWriter().write(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Getter And Setter
	 * 
	 */

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public Double getBmNum() {
		return bmNum;
	}

	public void setBmNum(Double bmNum) {
		this.bmNum = bmNum;
	}

	public String getBmContent() {
		return bmContent;
	}

	public void setBmContent(String bmContent) {
		this.bmContent = bmContent;
	}

	public BorrowMoney getBorrowMoney() {
		return borrowMoney;
	}

	public void setBorrowMoney(BorrowMoney borrowMoney) {
		this.borrowMoney = borrowMoney;
	}
	

}
